/* rule.vala
 *
 * Copyright (C) 2008  Frederik 'playya' Sdun
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author:
 * 	Frederik 'playya' Sdun <Frederik.Sdun@gmail.com>
 */
using GLib;
using Rob;

namespace Rob
{
    namespace Rules
    {
        //[DBus ( name="net.playya.rob" )]
        public interface IRule: GLib.Object
        {
            /*public string get_title();
            public void set_title(string t);
            public string get_name();
            public void set_name(string n);*/
            public abstract string name{ get; set;}
            public abstract string title{ get; set;}
            public abstract string type{get;construct;}
            public abstract bool running{get;}
            public abstract bool check();// throws DBus.Error;
            public abstract void run();// throws DBus.Error;
            public abstract void stop();// throws DBus.Error;
        }
        
        public class Rule : Rob.Observable, Rob.Rules.IRule, Rob.ILoadable
        {
            protected string _type;
            private bool is_running;
            //private DBus.Connection con;
            public bool running { get {return this.is_running;}}
            public string name{ get; set;}
            public string title{ get; set;}
            public virtual string type
            { 
                get{ return this._type;}
                construct{this._type = "INVALID";}
            }
            construct
            {
                string full_path = "%s/rules/%s".printf( Rob.BASE_OBJ_PATH,this.name  );
                Rob.connection.register_object( full_path, this );
            }
            
            public virtual bool check(){return true;}
            public virtual bool load(GLib.KeyFile kf, string name) throws GLib.KeyFileError
            {

                    this._name = name;
                    try
                    {
                    
                            this._type = kf.get_string(_name, "type");
                            this._title = kf.get_string(_name, "title");
                            return true;
                    }
                    catch( GLib.KeyFileError e)
                    {
                            GLib.critical("Could not load values for %s\n", this._name);		
                    }
                    return false;
            }
            public virtual bool save(GLib.KeyFile kf)
            {
                    kf.set_string(_name, "type", this._type );
                    kf.set_string(_name, "title", this._title );
                    return true;
            }
            public virtual void run(){}
            public virtual void stop(){}
        }

    }
}
